package Login;

import java.sql.*;



public class Test02 {
    public static void main(String[] args) {
        PreparedStatement pre=null;
        Connection conn =null;
        try{
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库连接对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8", "root", "x");
            //定义sql
            String sql = "select * from student where StuName = ? and Stupwd = ? ";
            //获取sql 执行对象，换成pre
            pre = conn.prepareStatement(sql);
            //要给点位符赋值
            pre.setString(1,"小向");
            pre.setString(2,"123456");
            //得到ResultSet
            ResultSet resultSet= pre.executeQuery();

            if(resultSet.next()){
                System.out.println(resultSet.getString("StuName"));
            }else {
                System.out.println("没有查询到数据");
            }

        }catch ( Exception e){
            System.out.println("xxxxxxxxxx");
            e.printStackTrace();//打印

        }finally {
            if(pre!=null){
                try{
                    pre.close();//关闭资源
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }

            if(conn!=null){
                try{
                    conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }

        }

    }
}
